/**
 * <h1>FunPhotoTaking</h1>
 * <a href="http://code.google.com/p/funphototaking/">Project Home</a>
 * @version 0.1 20090302
 */

import processing.core.*;
import controlP5.*;
import hypermedia.video.*;

public class FunPhotoTaking extends PApplet {
	ControlP5 controlP5;
	OpenCV opencv;
	
	/*
	 * 0 index
	 * 1 take photo
	 * 2 view photo
	 * 3 edit photo
	 * 4 share photo
	 */
	public int currentInterface;
	
	Textfield receiver;
	Textfield title;
	Textfield message;

	
	public void setup() {
		frameRate(12);
		size(800, 600);
		background(000000);
		controlP5 = new ControlP5(this);
		
		opencv = new OpenCV();
		opencv.capture(640, 480);
		
		
		currentInterface = 0;
		
		Controller exitBtn = controlP5.addButton("Exit",10, 100, 450, 100,25);
		exitBtn.setId(0);
		exitBtn.hide();
		
		// TAKE PHOTO
		controlP5.addBang("Take Photo", 150, 125, 250,150).setTriggerEvent(Bang.RELEASE);
		controlP5.controller("Take Photo").setId(1);

		controlP5.addBang("WebCamImage", 300, 150, 350,350).hide();
		controlP5.addButton("Capture",10, 100,150, 100,25).hide();
		controlP5.addButton("Cancel",10, 100,200, 100,25).hide();

		for(int i=0;i<7;i++){
			controlP5.addBang("effectItem"+i, 70+100*i, 50, 60 ,60).hide();  
		}

		// View Photo
		controlP5.addBang("View Photo", 425, 125, 250,150).setTriggerEvent(Bang.RELEASE);
		controlP5.controller("View Photo").setId(2);
		for(int i=0;i<5;i++){
			for(int j=0;j<3;j++)
				controlP5.addBang("ViewImage"+j+i, 250+100*i, 50+150*j, 90,120).hide();
		}

		// Edit PHOTO
		controlP5.addBang("Edit Photo", 150, 300, 250,150).setTriggerEvent(Bang.RELEASE);

		controlP5.controller("Edit Photo").setId(3);  
		controlP5.addBang("EditImage", 250, 100, 450,450).hide();
		controlP5.addButton("Save",10, 100,150, 100,25).hide();
		controlP5.addButton("Cancel2",10, 100,200, 100,25).hide();

		controlP5.addButton("Undo",10, 100,50, 50,25).hide();
		controlP5.addButton("Redo",10, 100,80, 50,25).hide();
		
		// share Photo
		controlP5.addBang("Share Photo", 425, 300, 250,150).setTriggerEvent(Bang.RELEASE);
		controlP5.controller("Share Photo").setId(4);

		controlP5.addButton("Send",10, 100,150, 100,25).hide();
		controlP5.addButton("Preview",10, 100,200, 100,25).hide();

		controlP5.addButton("ShareViaEmail",10, 250,100, 100,25).hide();
		controlP5.addButton("ShareViaInternet",10, 450, 100, 100,25).hide();

		receiver = controlP5.addTextfield("receiver",250,150,200,20);
		receiver.hide();
		title = controlP5.addTextfield("title",250,200,400,20);
		title.hide();
		message =controlP5.addTextfield( "message",  250,250,400,100);
		message.hide();
	}

	public void draw() {
		background(0,0,0);
		fill(50,50,50);
		noStroke();
		rect(0,0,800,25);
		rect(screen.width,0,500,400);
	}
	
	/**
	 * Toggle on/off a interface set.
	 * @param index
	 * @param on
	 */
	public void toggleInterface(int index, boolean on){
		switch(index){
			case 0: //index
				if(on){
					controlP5.controller("Take Photo").show();
					controlP5.controller("View Photo").show();
					controlP5.controller("Edit Photo").show();
					controlP5.controller("Share Photo").show();
				} else {
					controlP5.controller("Take Photo").hide();
					controlP5.controller("View Photo").hide();
					controlP5.controller("Edit Photo").hide();
					controlP5.controller("Share Photo").hide();
				}
				break;
			case 1: //take photo
				if(on){
					controlP5.controller("WebCamImage").show(); 
					controlP5.controller("Capture").show(); 
					controlP5.controller("Cancel").show(); 
					controlP5.controller("Exit").show(); 

					for(int i=0;i<7;i++){
						controlP5.controller("effectItem"+i).show();  
					}
				} else {
					controlP5.controller("WebCamImage").hide(); 
					controlP5.controller("Capture").hide();
					controlP5.controller("Cancel").hide();
					controlP5.controller("Exit").hide();

					for(int i=0;i<7;i++){
						controlP5.controller("effectItem"+i).hide();
					}
				}
				break;
			case 2: //view photo
				if(on){
					controlP5.controller("Exit").show();    
			    	for(int i=0;i<5;i++){
			    		for(int j=0;j<3;j++)
			    			controlP5.controller("ViewImage"+j+i).show();
			    	}
				} else {
					controlP5.controller("Exit").hide();    
			    	for(int i=0;i<5;i++){
			    		for(int j=0;j<3;j++)
			    			controlP5.controller("ViewImage"+j+i).hide();
			    	}
				}
				break;
			case 3: //edit photo
				if(on){
					controlP5.controller("EditImage").show(); 

			        controlP5.controller("Save").show(); 
			        controlP5.controller("Cancel2").show(); 
			        controlP5.controller("Exit").show(); 

			        controlP5.controller("Undo").show(); 
			        controlP5.controller("Redo").show();
				} else {
					controlP5.controller("EditImage").hide();

			        controlP5.controller("Save").hide();
			        controlP5.controller("Cancel2").hide();
			        controlP5.controller("Exit").hide();

			        controlP5.controller("Undo").hide();
			        controlP5.controller("Redo").hide();
				}
				break;
			case 4: //share
				if(on){
					controlP5.controller("ShareViaEmail").show(); 
			        controlP5.controller("ShareViaInternet").show(); 
			        controlP5.controller("Send").show(); 
			        controlP5.controller("Preview").show(); 
			        controlP5.controller("Exit").show(); 
			        message.show();
			        receiver.show();
			        title.show();
				} else {
					controlP5.controller("ShareViaEmail").hide();
			        controlP5.controller("ShareViaInternet").hide();
			        controlP5.controller("Send").hide();
			        controlP5.controller("Preview").hide();
			        controlP5.controller("Exit").hide();
			        message.hide();
			        receiver.hide();
			        title.hide();
				}
		}
		
	}
	
	public void controlEvent(ControlEvent theEvent) {
		int cID = theEvent.controller().id();
		if (cID >= 0 && cID <= 4){
			toggleInterface(currentInterface, false);
			toggleInterface(cID, true);
			currentInterface = cID;
		}
	}

	public void ShareViaEmail(){
		if(!receiver.isVisible()){
			receiver.show();
		}
	}

	public void ShareViaInternet(){
		if(receiver.isVisible()){
			receiver.hide();
		}
	}
	
	public static void main(String args[]) {
		PApplet.main(new String[] { "--present", "FunPhotoTaking" });
	}
}